/**
 *
 */
package com.zhuoyue.zhtj.web;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eit.eitsite.common.utils.IdGen;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.zhtj.entity.Hdgg;
import com.zhuoyue.zhtj.service.HdggService;

/**
 * 活动公告Controller
 * @author hss
 * @version 2017-08-17
 */
@Controller
@RequestMapping(value = "${adminPath}/zhtj/hdgg")
public class HdggController extends BaseController {

	@Autowired
	private HdggService hdggService;
	
	@ModelAttribute
	public Hdgg get(@RequestParam(required=false) String id) {
		Hdgg entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hdggService.get(id);
		}
		if (entity == null){
			entity = new Hdgg();
		}
		return entity;
	}
	
	@RequiresPermissions("zhtj:hdgg:view")
	@RequestMapping(value = {"list", ""})
	public String list(Hdgg hdgg, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Hdgg> page = hdggService.findPage(new Page<Hdgg>(request, response), hdgg); 
		model.addAttribute("page", page);
		return "zhuoyue/zhtj/hdggList";
	}

	@RequiresPermissions("zhtj:hdgg:view")
	@RequestMapping(value = "form")
	public String form(Hdgg hdgg, Model model) {
		model.addAttribute("hdgg", hdgg);
		return "zhuoyue/zhtj/hdggForm";
	}

	@RequiresPermissions("zhtj:hdgg:edit")
	@RequestMapping(value = "save")
	public String save(Hdgg hdgg, Model model, RedirectAttributes redirectAttributes,HttpServletRequest request) {
		if (!beanValidator(model, hdgg)){
			return form(hdgg, model);
		}
		hdggService.save(hdgg);
		addMessage(redirectAttributes, "保存活动公告成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/hdgg/?repage";
	}
	
	@RequiresPermissions("zhtj:hdgg:edit")
	@RequestMapping(value = "delete")
	public String delete(Hdgg hdgg, RedirectAttributes redirectAttributes) {
		hdggService.delete(hdgg);
		addMessage(redirectAttributes, "删除活动公告成功");
		return "redirect:"+Global.getAdminPath()+"/zhtj/hdgg/?repage";
	}

}